﻿Imports System.Data.SqlClient

Public Class SQLClass
    
    Public Shared Function SQLBackupDatabase(ByVal serverName As String, ByVal databaseName As String, ByVal saveDBLocation As String) As Boolean
        Dim con As SqlConnection
        Dim cmd As SqlCommand
        con = New SqlConnection("Data Source=" & serverName & ";Integrated Security=SSPI;Initial Catalog=" & databaseName & "")
        'con = New SqlConnection("workstation id=jokoatsnew.mssql.somee.com;packet size=4096;user id=jokoatsdb;pwd=joko1234;data source=jokoatsnew.mssql.somee.com;persist security info=False;initial catalog=jokoatsnew")
        cmd = New SqlCommand("backup database " & databaseName & " to disk='" & saveDBLocation & "'", con)
        Try
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            Return True
        Catch ex As Exception
            con.Close()
            EventLogClass.WriteToEventLog(ex.ToString, "SQLBackupDatabase", EventLogEntryType.Error, "ATS", 0)
            Return False
        End Try
    End Function

    Public Shared Function SQLRestoreDatabase(ByVal serverName As String, ByVal databaseName As String, ByVal DBBackupLocation As String) As Boolean
        Dim con As SqlConnection
        Dim cmd As SqlCommand
        con = New SqlConnection("Data Source=" & serverName & ";Integrated Security=SSPI;Initial Catalog=" & "Master" & "")


        Try
            con.Open()
            cmd = New SqlCommand("use master", con)
            cmd.ExecuteNonQuery()
            cmd = New SqlCommand("restore database " & databaseName & " from disk='" & DBBackupLocation & "' WITH REPLACE", con)
            cmd.ExecuteNonQuery()
            con.Close()
            Return True
        Catch ex As Exception
            con.Close()
            EventLogClass.WriteToEventLog(ex.ToString, "SQLRestoreDatabase", EventLogEntryType.Error, "ATS", 0)
            Return False
        End Try

    End Function

End Class



